var app = getApp();
var letters=require('../../utils/cityInfo.js').letters;
var WxSearch = require('../wxSearch/wxSearch.js')
Page({
    data: {
        loading: true,
 
        loadtxt: '正在加载',
        letterId:'',
        isShowLetter:false,//是否显示字母
        showLetter: ""//显示字母内容
    },

    onLoad:function(){
        this.type=arguments[0].type;
        var that=this;
 
        app.get('/BusTicketInfo.go',{
            method:'queryEndStationInfo',
            proxyId:app.globalData.proxyid,
            query:''

        }).then(function(data){
            if(data.totalCount>0){

                var city=that.getCityList(data.rows);
                WxSearch.init(that,43,data.rows,false,false);
                that.setData({
 
                    endstation:city.cityList,
                    endstationLetters:city.newLetters
                    
                })
            }
           that.setData({
                loading: false  
            })
        }).catch(function(e){
            console.log(e)
        })

 
    },
    //得到到达站列表
    getCityList:function(list){
        var newLetters=[];
        var cityList={};
        letters.forEach(function(le){
            var count=0;
            (list||[]).forEach(function(li){
                if(li.station_input_code.substr(0,1).toUpperCase()==le){
                    count++;
                    cityList[le]==undefined&&(cityList[le]=[]);
                    cityList[le].push(li);
                }
            })
            count&&newLetters.push(le);
        })

        return {
            newLetters:newLetters,
            cityList:cityList
        }
    },

    handleLetterTap:function(ev){
        var id=ev.target.dataset.id;
        this.setData({
            letterId:id
            
        });
     
    },
   searchStart: function (e) {
    var showLetter = e.currentTarget.dataset.id;
 
      this.setData({
        showLetter: showLetter,
        isShowLetter:true,
        letterId:showLetter
      })
  },
   searchEnd: function (e) {
 
    var that=this;
    setTimeout(function(){
      that.setData({
      isShowLetter:false
    })
    },1000)
    
  },



  
    bindChangeCity: function(e) { //绑定返回的页面
      var city = e.currentTarget.dataset.city;
      var cityid= e.currentTarget.dataset.cityid;
      
      var fn = app.globalData.cityFn;
      var type=this.type;
      if (typeof fn == 'function') {
        setTimeout(function(){
          fn(city,cityid,type);
          app.globalData.cityFn=null;
        },0)
        wx.navigateBack({
          delta: 1
        });
      }
    },
    
  wxSearchFn: function(e){
    var that = this
    WxSearch.wxSearchAddHisKey(that);
    
  },
 //搜索
  wxSearchInput: function(e){
 
    var that = this
    WxSearch.wxSearchInput(e,that);
 
  },
  wxSerchFocus: function(e){

    var that = this
    WxSearch.wxSearchFocus(e,that);
 
  },
  wxSearchBlur: function(e){
    var that = this
    WxSearch.wxSearchBlur(e,that);


  },
  //点击搜索列表
  wxSearchKeyTap:function(e){
    var that = this
    WxSearch.wxSearchKeyTap(e,that);
    this.bindChangeCity(e);
  },
  wxSearchDeleteKey: function(e){
    var that = this
    WxSearch.wxSearchDeleteKey(e,that);
  },
  wxSearchDeleteAll: function(e){
    var that = this;
    WxSearch.wxSearchDeleteAll(that);
  },
  //隐藏搜索列表
  wxSearchTap: function(e){
    var that = this
    WxSearch.wxSearchHiddenPancel(that);

    }
})

// Initial Spell CityCode 
